/*
 * @Author: yangzonglong
 * @Date: 2021-07-08 14:12:22
 * @version: v1.0.0
 * @Descripttion: Table Radio 控制器
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-07-25 12:43:13
 * @Auditor:
 */
import React, { useCallback } from 'react';
import update from 'immutability-helper';
import CommonController from '../CommonController';
import OptionSetting from './../../components/optionSetting/OptionSetting';
import { IProps } from '../config';

const RadioController = ({
  tableProps,
  dataSource,
  print,
  required,
  originalIndex,
  columns,
  dataIndex,
  title,
  titleEn,
  width,
  disabled,
  exportFlag,
  changeColumnOptions,
  changeControlProps,
  setCurColumn,
  fontSize,
  fontWeight,
  backgroundColor,
  color,
  customizeStyleFlag
}: IProps) => {
  const onChangeColumnOptions = useCallback(
    (e: any, k: string, isChecked?: boolean) => {
      changeColumnOptions(k, isChecked ? e.target.checked : e.target.value, originalIndex);
    },
    [changeColumnOptions, originalIndex]
  );

  const _changeControlProps = useCallback(
    (e: any, k: string, isChecked?: boolean) => {
      changeControlProps(
        'tableProps',
        {
          ...tableProps,
          columns: update(columns!, {
            [originalIndex!]: {
              [k]: {
                $set: isChecked ? e.target.checked : e.target.value,
              },
            },
          }),
          dataSource: { $set: dataSource },
        },
        true
      );
      setCurColumn?.({ ...columns![originalIndex!], originalIndex, [k]: isChecked ? e.target.checked : e.target.value });
    },
    [changeControlProps, tableProps, columns, originalIndex, dataSource, setCurColumn]
  );

  return (
    <CommonController
      onChangeControlProps={_changeControlProps}
      onChangeColumnOptions={onChangeColumnOptions}
      {...{ title, titleEn, dataIndex, width, required, print, disabled, exportFlag, fontSize, fontWeight, backgroundColor, color, customizeStyleFlag }}
    >
      <OptionSetting showDefault changeControlProps={(key, value) => changeColumnOptions(key, value, originalIndex)} options={{ dataSource }} />
    </CommonController>
  );
};

export default RadioController;
